package com.cloudm.technician.ui.modules.cloudm_box.cloudm_device

import com.cloudm.technician.data.model.DeviceInfo
import com.cloudm.technician.data.network.CloudmBoxApi
import com.cloudm.technician.ui.base.RxPresenter

/**
 * 作者: wesley
 * 时间: 2017/12/25
 * 描述: 业务逻辑
 */
class CloudmDevicePresenter(private val view: CloudmDeviceContract.View,
                            private val cloudmBoxApi: CloudmBoxApi) : RxPresenter(),
        CloudmDeviceContract.Presenter {

    override fun getDeviceInfo(sn: String?) {
        request(cloudmBoxApi.getDeviceInfo(sn), view, onSuccess = { info, _ ->
            when (info) {
                null -> {
                    view.showDeviceInfoEmpMsg()
                    view.finishSelf()
                }

                else -> {
                    view.deviceInfoLoadSuccess(info)
                }
            }
        }, onFailure = { errorMsg ->
            view.deviceInfoLoadFailure(errorMsg)
            view.finishSelf()
        })
    }

    override fun openActivity(info: DeviceInfo?) {
        if(info == null){
            view.showLoadingMsg()
            return
        }
        view.openActivity(info)
    }

}